package models

import (
	"context"
	"go-chain-api/global"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type ScanConfig struct {
	ChainName       string `json:"chainName"`
	ChainID         int64  `json:"chainId"`
	RpcUrl          string `json:"rpcUrl"`
	LastBlockNumber int64  `json:"lastBlockNumber"`
	UdunId          int64  `json:"udunId"`
	FactoryAddress  string `json:"factoryAddress"`
	UsdtAddress     string `json:"usdtAddress"`
	RouterAddress   string `json:"routerAddress"`
	MainAddress     string `json:"mainAddress"`
}

func GetConfigData(chainName string, chainId uint, typeVal string) (ScanConfig, error) {
	var data ScanConfig
	configClient := global.DBEngine.Database(global.DbConfig.DbName).Collection("scan_config")
	findOptions := options.FindOne()
	findOptions.SetSort(bson.D{{"_id", -1}})
	var filter bson.M
	if typeVal != "" {
		filter = bson.M{"chainName": chainName, "chainId": chainId, "type": typeVal}
	} else {
		filter = bson.M{"chainName": chainName, "chainId": chainId}
	}
	err := configClient.FindOne(context.Background(), filter, findOptions).Decode(&data)
	return data, err

}
